from flask import Flask, render_template, redirect, url_for
from flask_script import Manager
from flask_cors import CORS
# from flask_wtf.csrf import CSRFProtect

from views.api import api
from views.admin import admin
from views.view import view

app = Flask(__name__)

app.config['SECRET_KEY'] = 'python'
cors = CORS(app, resources={"/api/*": {"origins": "*"}, "/admin/*": {"origins": "*"}})
# CSRFProtect(app)

app.register_blueprint(blueprint=api, url_prefix='/api')
app.register_blueprint(blueprint=admin, url_prefix='/admin')
app.register_blueprint(blueprint=view)


@app.route('/demo')
def creating():
    return redirect(url_for('admin.index'), 301)


@app.errorhandler(404)
def miss(e):
    return render_template('404.html'), 404


@app.errorhandler(500)
def error(e):
    return render_template('500.html'), 500


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return render_template("index.html")


# step1 定义过滤器
@app.template_filter('list_reverse')  # 使用装饰器
def do_list_reverse(li):
    temp_li = list(li)
    temp_li.reverse()
    return temp_li


# 将flask对象交给Manager管理，并且启动方式修改成manager.run()
manager = Manager(app=app)

if __name__ == '__main__':
    # 修改启动的IP和端口,debug模式
    # Debugger PIN: 151-498-317  这是debug的码
    # app.run(host='127.0.0.1', port=5003, debug=True)

    # python manager.py runserver -p 5002 -h 127.0.0.1 -d
    manager.run()
